﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ArmoryViewerMobile
{
    class IniReader
    {
        FileStream fs;
        String path;
        private StreamReader iniRead;
        private StreamWriter iniWrite;
        private String fileContent;
        public IniReader(String FilePath)
        {
            path = FilePath;
            readFile();
        }

        private void readFile()
        {
            fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            iniRead = new StreamReader(fs);
            fileContent = iniRead.ReadToEnd();
            iniRead.Close();
            fs.Close();
        }

 
        private object Read(string Section, string Key, object DefaultValue)
        {
            int secInd = fileContent.IndexOf("[" + Section + "]");
            if (secInd == -1) { return DefaultValue; }
            String values = fileContent.Substring(secInd + ("[" + Section + "]").Length + 1);
            if (values.IndexOf("[") != -1)
                values = values.Substring(0, values.IndexOf("["));

            while (values.IndexOf("\n") != -1)
            {
                String line = values.Substring(values.IndexOf("\n") + 1);
                line = line.Substring(0, line.IndexOf("\r"));
                if (line.Replace(" ", "").StartsWith(Key + "="))
                {
                    char[] trim = {' '};
                    line = line.Substring(line.IndexOf("=") + 1).TrimStart(trim);
                    return line;
                }
                values = values.Substring(line.Length + 1);
            }

            return DefaultValue;
        }

        public String ReadString(string Section, string Key, string DefaultValue)
        {
            return Read(Section, Key, DefaultValue).ToString();
        }

        public bool ReadBoolean(string Section, string Key, bool DefaultValue)
        {
            try
            {
                return Convert.ToBoolean(Read(Section, Key, DefaultValue).ToString());
            }
            catch
            {
                return DefaultValue;
            }
        }

        public int ReadInteger(string Section, string Key, int DefaultValue)
        {
            try
            {
                return Convert.ToInt32(Read(Section, Key, DefaultValue).ToString());
            }
            catch
            {
                return DefaultValue;
            }
        }

        public void Write(string Section, string Key, object value)
        {
            fs.Close();
            fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            iniWrite = new StreamWriter(fs);
            try
            {
                int secInd = fileContent.IndexOf("[" + Section + "]");
                if (secInd == -1) 
                {
                    fileContent += Environment.NewLine +
                        "[" + Section + "]" + Environment.NewLine +
                        Key + "=" + (String)value;
                    iniWrite.Write(fileContent);
                    iniWrite.Close();
                    fs.Close();
                    return;
                }
                String values = fileContent.Substring(secInd + ("[" + Section + "]").Length + 1);
                if (values.IndexOf("[") != -1)
                    values = values.Substring(0, values.IndexOf("["));

                String valuesOrig = values;
                while (values.IndexOf("\n") != -1)
                {
                    String line = values.Substring(values.IndexOf("\n") + 1);
                    if (line.Replace(" ", "").StartsWith(Key + "="))
                    {
                        char[] trim = { ' ' };
                        String val = line.Substring(line.IndexOf("=") + 1).TrimStart(trim);
                        String newLine = line.Replace(val, (String)value);
                        String newValues = values.Replace(line, newLine);
                        fileContent = fileContent.Replace(valuesOrig, newValues);
                        iniWrite.Write(fileContent);
                        iniWrite.Close();
                        fs.Close();
                        return;
                    }
                    values = values.Substring(line.Length + 1);
                }
                String newVals = valuesOrig;
                newVals += Environment.NewLine +
                    Key + "=" + value;
                fileContent = fileContent.Replace(valuesOrig, newVals);
                iniWrite.Write(fileContent);
                iniWrite.Close();
                fs.Close();

            }
            catch
            {
                return;
            }
        }
    }
}
